﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Admin.SystemSet
{
    public partial class MoneyCardList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(AdminUPager);

            LoadCondition();

            string cardno = txtNo.Text.Trim();
            string status = ddlStatus.SelectedValue;

            MoneyCardHelper mchelper = new MoneyCardHelper();
            Criteria c = new Criteria();

            if (!string.IsNullOrEmpty(cardno))
            {
                c.Add(CriteriaType.Like, "MoneyCardNo", "%" + cardno + "%");
            }
            if (status != "-1")
            {
                c.Add(CriteriaType.Equals, "Status", status);
            }

            AdminUPager.ItemCount = mchelper.QueryMoneyCardCountBySearch(c);

            // 以下的值从BasePage中得到
            AdminUPager.UrlFormat = UrlFormat;
            AdminUPager.PrefixText = GetPreFixText(AdminUPager);

            if (mark)
            {
                PagerInit(AdminUPager);
            }

            string[] fields = new string[] { "ID", "MoneyCardNo", "Money", "Status", "Flag", "UsedDate", "CreateDate" };

            DataTable dt = new DataTable();
            dt = mchelper.QueryMoneyCardBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, AdminUPager.Begin - 1, AdminUPager.Count, fields);

            dt.Columns.Add(new DataColumn("strStatus"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string getstatus = dr["status"] + "";
                    if (getstatus == "0")
                    {
                        dr["strStatus"] = "未使用";
                    }
                    else if (getstatus == "1")
                    {
                        dr["strStatus"] = "已使用";
                    }
                }
            }

            rptAdmin.DataSource = dt;
            rptAdmin.DataBind();
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }

        #region 查询条件设置
        private void SetCondition()
        {
            string cardno = txtNo.Text.Trim();
            string status = ddlStatus.SelectedValue;

            //将查询条件放入Session中
            Session["QueryList"] = cardno + "@" + status + "@" + "MoneyCardList";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 3)
            {
                string page = conditionAll[2];
                if (page == "MoneyCardList")
                {
                    txtNo.Text = conditionAll[0];
                    ddlStatus.SelectedValue = conditionAll[1];
                }
            }
        }
        #endregion
    }
}